package com.google.code.dweibo.util;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;

/**An IO util class
 * 
 * @author jessiejacky
 *
 */
public class IOUtil {
	/**Read the file into a byte array
	 * 
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public static byte[] getByteArray(String file) throws Exception {
		DataInputStream dis = null;
		ByteArrayOutputStream ba = null;
		DataOutputStream dos = null;

		try {
			dis = new DataInputStream(new FileInputStream(file));
			ba = new ByteArrayOutputStream();
			dos = new DataOutputStream(ba);

			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = dis.read(buffer, 0, 1024)) != -1) {
				dos.write(buffer, 0, len);
				dos.flush();
			}
			return ba.toByteArray();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw e;
		} finally{
			if(dis != null){
				dis.close();
			}
			if(dos != null){
				dos.close();
			}
			if(ba != null){
				ba.close();
			}
		}
	}
}
